#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -d sqlite-3.6.22 ] ; then
		wget http://www.sqlite.org/sqlite-amalgamation-3.6.22.tar.gz
		tar xzf sqlite-amalgamation-3.6.22.tar.gz
	fi
	cd ..
fi

export PATH=$TOP_DIR/cross/bin:$PATH

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
	fi
	rm -rf $BIN_DIR/sqlite
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_sqlite
	mkdir bd_sqlite
	cd bd_sqlite
	$SRC_DIR/sqlite-3.6.22/configure $HOST_FLAG --prefix=$BIN_DIR/sqlite \
			--enable-shared --disable-static

#	--enable-static --disable-shared

	make
	make install
	cd ..
	cd ..
fi

